package com.lclz.service.security;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.map.LazyMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AuthenticationUtil {
	static final Logger LOG = LoggerFactory.getLogger(AuthenticationUtil.class);
	// private static final Logger logger = Logger.getLogger(AuthenticationUtil.class);
	private static final String DEFAULT_FILENAME = "/authentication.properties";
	private static LinkedHashMap<Object, Object> configMaps = new LinkedHashMap<Object, Object>();

	/**
	 * 加载配置文件
	 */
	private static void loadConfig() {
		// 日志
		long startTime = System.currentTimeMillis();
		String logParam = "";
		try {
			Properties prop = new Properties();
			prop.load(AuthenticationUtil.class.getResourceAsStream(DEFAULT_FILENAME));
			Iterator<Entry<Object, Object>> itr = prop.entrySet().iterator();
			while (itr.hasNext()) {
				Entry<Object, Object> e = itr.next();
				configMaps.put(e.getKey(), e.getValue());
			}
		} catch (IOException e1) {
			logParam = e1.toString();
			// TranLog.tranLog(TranEnum.EXAMLIST_CURRPAPER, startTime, resultEnum, logParam, "error");
		}
	}

	private static Object getProperty(String key) {
		// 日志
		long startTime = System.currentTimeMillis();
		String logParam = "";
		try {
			Properties prop = new Properties();
			prop.load(AuthenticationUtil.class.getResourceAsStream(DEFAULT_FILENAME));
			return prop.get(key);
		} catch (IOException e) {
			logParam = e.toString();
			// TranLog.tranLog(TranEnum.EXAMLIST_CURRPAPER, startTime, resultEnum, logParam, "error");
		}
		return null;
	}

	/**
	 * @info : 描述 获取配置文件值
	 * @auth : lhli
	 * @date : 2013-7-31 下午4:24:00
	 * @param key
	 * @return
	 */
	public static Object getConfigValue(String key) {
		if (CheckUtil.isEmpty(configMaps))
			loadConfig();
		@SuppressWarnings("unchecked")
		Map<String, Object> lazy = LazyMap.decorate(configMaps, new Transformer() {
			@Override
			public Object transform(Object input) {
				return getProperty(input.toString());
			}
		});
		return lazy.get(key);
	}

	public static void main(String[] args) {
		getConfigValue("sdf");
	}
}
